/*****************************************************************************//*!
* file  : systask.c
* creat : apleilx
* data  : 2017/11/05
*******************************************************************************/

#include "osObjects.h"
#include "pincfg.h"
#define _SYSTASK_MODULE_
#include "systask.h"
#undef _SYSTASK_MODULE_
#include "bsp_dwt.h"

uint32_t SysTaskCnt;
uint32_t PowerTimeCnt;
uint8_t SysTaskAutoCnt;

void SysTimePulseCreat(void);

/*****************************************************************************//*!
*
* @brief   storage task.
*     
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void SysTask(void const *argument)
{
    for (;;){
		bsp_dwt_check();
        
        SysTimePulseCreat();
        SysTaskCnt++;

        if (bSysPulse_500ms){
            LED_CPU = !LED_CPU;
        }
		
		osSemaphoreWait(sid_tst, 0);
		osSemaphoreWait(sid_tst, 0);
		api.sem_req = 1;
		osSemaphoreWait(sid_tst, 10000);
		api.sem_rcv_tick = bsp_cpu_clks_get();
		api.sem_tick_used = api.sem_rcv_tick - api.sem_snd_tick;
			
		if(api.mutex_busy)
		{
			api.mutex_req = 1;
			api.mutex_busy = 0;
			osMutexWait(mid_tst,10000);
			api.mutex_rcv_tick = bsp_cpu_clks_get();
			api.mutex_tick_used = api.mutex_rcv_tick - api.mutex_snd_tick;
		}
		
		osMessageGet(qid_tst, 0);
		osMessageGet(qid_tst, 0);
		osMessageGet(qid_tst, 0);
		
		api.quene_req = 1;
		osMessageGet(qid_tst, 10000);
		api.quene_rcv_tick = bsp_cpu_clks_get();
		api.quene_used = api.quene_rcv_tick - api.quene_snd_tick;
		
        osDelay(50);
        INTX_DISABLE();
        INTX_ENABLE();
    }
}


void ApiTask(void const *argument)
{
	while(1)
	{
		
		if(api.sem_req)
		{
			api.sem_req = 0;
			api.sem_snd_tick = bsp_cpu_clks_get();
			osSemaphoreRelease(sid_tst);
		}
		
			if(!api.mutex_busy)
			{
				osMutexWait(mid_tst,0);
				api.mutex_busy = 1;
			}
			if(api.mutex_req)
			{
				api.mutex_req = 0;
				api.mutex_snd_tick = bsp_cpu_clks_get();
				osMutexRelease(mid_tst);
				
			}		
		
		if(api.quene_req)
		{
			api.quene_req = 0;
			api.quene_snd_tick = bsp_cpu_clks_get();
			osMessagePut(qid_tst, 0x0005, 0);
		}
		
		osDelay(2);
	}
}


/*****************************************************************************//*!
*
* @brief   system time pulse creat.
*     
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
extern uint32_t uwTick;
void SysTimePulseCreat(void){
    static uint32_t uwTickLast;
    volatile uint32_t tick;
    
    uint16_t TickInc;
    
    tick = uwTick;
    TickInc = tick  - uwTickLast;
    uwTickLast = uwTick;

    bSysPulse_1s = 0;
    bSysPulse_500ms = 0;
    bSysPulse_250ms = 0;
    bSysPulse_100ms = 0;
    
    if(!TickInc)
        return;
    
    SysPulseTimeCnt_1s+=TickInc;
    SysPulseTimeCnt_500ms+=TickInc;
    SysPulseTimeCnt_250ms+=TickInc;
    SysPulseTimeCnt_100ms+=TickInc;
    
    if(SysPulseTimeCnt_1s >= (1000/1)){
        SysPulseTimeCnt_1s-= (1000/1);
        bSysPulse_1s = 1;
    }
    
    if(SysPulseTimeCnt_500ms >= (500/1)){
        SysPulseTimeCnt_500ms-= (500/1);
        bSysPulse_500ms = 1;
    }
    
    if(SysPulseTimeCnt_250ms >= (250/1)){
        SysPulseTimeCnt_250ms-= (250/1);
        bSysPulse_250ms = 1;
    }
    
    if(SysPulseTimeCnt_100ms >= (100/1)){
        SysPulseTimeCnt_100ms-= (100/1);
        bSysPulse_100ms = 1;
        
        SysTaskAutoCnt++;
        if(PowerTimeCnt < INT32_MAX)
            PowerTimeCnt++;
    }
}

